// pages/work9/index.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
      isincome:"true",
      incomelist:[],
      paylist:[],
      datelist:[],
      datelist1:[],
      isinput:true,
      incometotal:0,
      paytotal:0,
      inputval:'',
      date:'',
      money:'0'
  },
  num:0,
  chanagedate(e){
      console.log(e)
      let datelist=this.data.datelist
      let datelist1=this.data.datelist1
      let date=e.detail.value.substring(6,10)
      console.log(datelist1)
      if(!datelist1.includes(date)){
          datelist.push({"date":date,"shou":0,"zhi":0})
          datelist1.push(date)
      }
      
      this.setData({
          date,
          datelist,
          datelist1
      })
      this.num=datelist1.indexOf(date)
      console.log(this.num)
  },

  add(){
      this.setData({
          isinput:!this.data.isinput
      })
  },
  click(e){
      console.log(e)
      if(e.currentTarget.dataset.name=='income'){
          this.setData({
             isincome:true
          })
      }else{
          this.setData({
              isincome:false
           })
      }
  },

  confirm(e){
      console.log(e)
      let je=Number(e.detail.value)
      let incomelist=this.data.incomelist
      let paylist=this.data.paylist
      let datelist=this.data.datelist
      if(this.data.isincome){
          incomelist.push(je)
          let incometotal=incomelist.reduce((sum,v)=>{
              return sum+=v
          },0)
          datelist[this.num].shou+=je
          this.setData({
              incomelist,
              incometotal,
              inputval:'',
              isinput:false,
              datelist
          })
      }else{
          paylist.push(je)
          let paytotal=paylist.reduce((sum,v)=>{
              return sum+=v
          },0)
          datelist[this.num].zhi+=je
          this.setData({
              paylist,
              paytotal,
              inputval:'',
              isinput:false,
              datelist
          })
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that=this
      wx.getStorage({
          key:'money',
          success(e){
          that.setData({
              incomelist:e.data[0],
              paylist:e.data[1],
              incometotal:e.data[2],
              paytotal:e.data[3],
              datelist:e.data[4],
              date:e.data[5],
              datelist1:e.data[6]
          })
      }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      let store=[this.data.incomelist,this.data.paylist,this.data.incometotal,this.data.paytotal,this.data.datelist,this.data.date,this.data.datelist1]
      wx.setStorage({
          data:store,
          key:'money'
      })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      this.onHide()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})